<template>
  <div class="app-container">
    总提现：{{ totalRecharge }} <br/>
    搜索:
    <el-input v-model="keyword" style="width: 200px; padding: 10px" placeholder="输入姓名/手机号"></el-input>
    <div class="block">
      <el-date-picker
        v-model="filterTime.date"
        type="daterange"
        align="right"
        value-format="yyyy-MM-dd"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="dateFormat"
        :picker-options="pickerOptions">
      </el-date-picker>
    </div>
    <span> 提现状态： </span>
    <template>
      <el-select v-model="status" placeholder="请选择状态"  style="margin: 10px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>
    <el-button type="primary" style="margin-top: 20px" @click="search">搜索</el-button>
    <el-button type="primary" style="margin-top: 20px" @click="downLoad">导出</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >

      <el-table-column align="center" label="id" width="110">

        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户id" width="110">

        <template slot-scope="scope">
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="150">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>

      <el-table-column label="真实姓名" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.realName }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.cardNo }}
        </template>
      </el-table-column>

      <el-table-column label="手机号" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.cardNumber }}
        </template>
      </el-table-column>
      <el-table-column label="应发金额" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column label="提现状态" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.status === 1? '已提现' : '' }}
          {{ scope.row.status === 0? '审核中' : '' }}
          {{ scope.row.status === -1? '已驳回' : '' }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="提现时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="审核" width="110" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 0" size="mini" type="primary" round @click="dialogFormVisible = true; verifyWithdrawal.id=scope.row.id">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="提现审核" :visible.sync="dialogFormVisible">
      <el-radio v-model="verifyWithdrawal.status" label="1">通过</el-radio>
      <el-radio v-model="verifyWithdrawal.status" label="-1">拒绝</el-radio>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelWithdraw">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData">
    </el-pagination>
  </div>
</template>

<script>
import { getWithdrawList,applyWithdraw,getTotalWithdraw } from '@/api/userBill'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      associationDTO: {
        avatar: '',
        presidentAccount: '',
        name: '',
        intro: '',
        commissionRatio: 0.15
      },
      dialogFormVisible: false,
      verifyWithdrawal: {
        status: 0,
        id: null
      },
      totalWithdrawal: null,
      options: [{
        value: null,
        label: '全部'
      }, {
        value: '0',
        label: '审核中'
      }, {
        value: '1',
        label: '已通过'
      }, {
        value: '-1',
        label: '被驳回'
      }],
      filterTime: {
        startTime: '',
        endTime: '',
        date: ''
      },
      keyword: null,
      dialogCreateAssociationVisible: false,
      list: null,
      listLoading: true,
      status: null,
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    dateFormat(picker) {
      this.filterTime.startTime = picker[0]
      this.filterTime.endTime = picker[1]
    },
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getWithdrawList({ page: this.page, size: this.size, status: this.status, keyword: this.keyword,startTime: this.filterTime.startTime, endTime: this.filterTime.endTime }).then(response => {
        console.log(response)
        this.total = response.total
        this.list = response.data
        this.listLoading = false
      })
      getTotalWithdraw({keyword: this.keyword, status: this.status,startTime: this.filterTime.startTime, endTime: this.filterTime.endTime}).then(res=>{
        this.totalRecharge = res.data.totalWithdraw
      })
    },
    search() {
      this.fetchData(this.page)
      getTotalWithdraw({keyword: this.keyword, status: this.status,startTime: this.filterTime.startTime, endTime: this.filterTime.endTime}).then(res=>{
        this.totalRecharge = res.data.totalWithdraw
      })
    },
    downLoad() {
      window.location.href = 'http://localhost:8091/admin-api/userBill/exportList?page=' + this.page + '&size=' + this.size + '&status=' + this.status + '&keyword=' + this.keyword
      this.$message.success('导出成功')
    },
    handleRemove(file) {
      this.associationDTO.avatar = ''
      console.log(file)
    },
    handlePictureCardPreview(response, file) {
      this.associationDTO.avatar = response.data.url
    },
    handelWithdraw() {
      this.$confirm('确认完成提现?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        applyWithdraw(this.verifyWithdrawal).then(() => {
          this.dialogFormVisible = false
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '操作失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // realDeleteById(id).then(() => {
        //   this.fetchData(this.page)
        //   this.$message({
        //     type: 'success',
        //     message: '删除成功!'
        //   })
        // }).catch(() => {
        //   this.$message({
        //     type: 'error',
        //     message: '删除失败'
        //   })
        // })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
